package com.googlecode.jreview;

// Todo - update the imports to get just what we need
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class jrGui
{
	//--- INSTANCE VARIABLES ---
	JFrame mainWindow = new JFrame("jReview");
	
	static final int GUI_WIDTH = 600;
	static final int GUI_HEIGHT = 375;
	
	// help maintain state when entering new questions
	//	(there's two screens for doing that)
	String questionContent;
	String questionAnswer;
	JTextArea questionArea = new JTextArea();
	JTextArea answerArea = new JTextArea();
	ArrayList<String> wrongAnswers;
	
	// We use a frame which spans the whole of the window so that we can toggle visibility,
	// or perform other operations, without creating the appearance of the program exiting
	// and restarting.
	JPanel mainPanel = new JPanel();
	JMenuBar menuBar = new JMenuBar();
	// menu helpers:
	JMenu menu;
	JMenuItem menuItem;
	JRadioButtonMenuItem rbMenuItem;
	
	public void createAndShow()
	{
		// Initialize
		mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//mainWindow.setResizable(false); might use this on final version, but keep it resizable for debugging gui
		mainWindow.setSize(GUI_WIDTH, GUI_HEIGHT);
		mainWindow.setLocation(10, 10);
		mainWindow.getContentPane().add(BorderLayout.CENTER, mainPanel);
		mainPanel.setLayout(new BorderLayout());
		
		//-- MENU --
			// jReview
			menu = new JMenu("jReview");
			
			menuItem = new JMenuItem("Clean the GUI");
			menuItem.addActionListener(new CleaningListener());
			menu.add(menuItem);
			
			menuItem = new JMenuItem("About");
			menuItem.addActionListener(new AboutListener());
			menu.add(menuItem);
			
			menuItem = new JMenuItem("Quit");//new ImageIcon("com/googlecode/jreview/img/quit.png"));
			menuItem.addActionListener(new QuitListener()); // wait til we have icons for everything
			menu.add(menuItem);
			
			menuBar.add(menu);
			
			// Lists
			// Not yet up to date on my list-editing capabilities.
			/*menu = new JMenu("Lists");
			
			menuItem = new JMenuItem("Load list");
			menu.add(menuItem);
			
			menuItem = new JMenuItem("Make new list");
			menuItem.addActionListener(new NewQuestionListener());
			menu.add(menuItem);
			
			menuItem = new JMenuItem("Edit old list");
			menu.add(menuItem);
			
			menuBar.add(menu);*/
			
			// Settings
			menu = new JMenu("Settings");
			
			menuItem = new JMenuItem("Change active lists");
			menu.add(menuItem);
			
			menuItem = new JMenuItem("Change frequency settings");
			menu.add(menuItem);
			
			menu.addSeparator();
			
				// Enable / Disable
				ButtonGroup group = new ButtonGroup();
				rbMenuItem = new JRadioButtonMenuItem("jReview Enabled");
				rbMenuItem.setSelected(true);
				group.add(rbMenuItem);
				menu.add(rbMenuItem);
				
				rbMenuItem = new JRadioButtonMenuItem("jReview Disabled");
				group.add(rbMenuItem);
				menu.add(rbMenuItem);
				
			menuBar.add(menu);
		//--- END MENU ---
		
		// Show		
		mainWindow.setJMenuBar(menuBar);
		mainWindow.setVisible(true);
	} //--- END CREATE AND SHOW ---
	
	
	public void newQuestionGUI()
	{
		cleanGUI();
		
		//--- PANELS ---
		
		// Top panel
		JPanel topPanel = new JPanel();
		
		// Bottom panel
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));
		
		//--- END PANELS ---
		
		//--- COMPONENTS ---
			// Question Area
			questionArea.setFont(new Font("Serif", Font.ITALIC, 16));
			questionArea.setLineWrap(true);
			questionArea.setWrapStyleWord(true);
			JScrollPane qPane = new JScrollPane(questionArea);
			qPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			qPane.setPreferredSize(new Dimension((GUI_WIDTH - 40), ((GUI_HEIGHT / 2) - 54)));
			qPane.setBorder(
				BorderFactory.createCompoundBorder(
					BorderFactory.createTitledBorder("Question"),
					BorderFactory.createEmptyBorder(5, 5, 5, 5)));

			// Answer Area
			answerArea.setFont(new Font("Serif", Font.PLAIN, 16));
			answerArea.setLineWrap(true);
			answerArea.setWrapStyleWord(true);
			JScrollPane aPane = new JScrollPane(answerArea);
			aPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			aPane.setPreferredSize(new Dimension((GUI_WIDTH - 40), ((GUI_HEIGHT / 2) - 54)));
			aPane.setMaximumSize(new Dimension((GUI_WIDTH - 40), ((GUI_HEIGHT / 2) - 54)));
			aPane.setBorder(
				BorderFactory.createCompoundBorder(
					BorderFactory.createTitledBorder("Correct Answer"),
					BorderFactory.createEmptyBorder(5, 5, 5, 5)));
			aPane.setAlignmentX(Component.CENTER_ALIGNMENT);

			// "Next" (pushing this will sanity-check Q&A, and bring up a screen for multiple choice
			JButton nextButton = new JButton("Next");
			nextButton.setFocusable(false);
			nextButton.addActionListener(new MCEnterListener());
			nextButton.setAlignmentX(Component.CENTER_ALIGNMENT);
					
		//--- END COMPONENTS ---
		
		// Organize panels
			
		// Add components to the panels
		topPanel.add(qPane);
		bottomPanel.add(aPane);
		bottomPanel.add(Box.createRigidArea(new Dimension(0, 6)));
		bottomPanel.add(nextButton);
		bottomPanel.add(Box.createRigidArea(new Dimension(0, 4)));
		
		// Add top-level panels to the GUI
		mainPanel.add(BorderLayout.NORTH, topPanel);
		mainPanel.add(BorderLayout.SOUTH, bottomPanel);

		// Refresh
		mainPanel.setVisible(false);
		mainPanel.setVisible(true);
		
	}
	public void MCEnterGUI()
	{
		cleanGUI();
		
		// Panels
		JPanel explanation = new JPanel();
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BorderLayout());
		JPanel buttons = new JPanel();
		buttons.setBorder(
			BorderFactory.createEmptyBorder(5, 5, 5, 5));
		
		GridLayout grid = new GridLayout(2, 2, 4, 4);
		JPanel answers = new JPanel(grid);
		
		// Components
		JLabel explanationLabel = new JLabel("<html>Please enter a few <i>reasonable, but incorrect</i> answers below.");
		explanationLabel.setBorder(
			BorderFactory.createEmptyBorder(2, 2, 6, 2));
		
		JLabel questionLabel = new JLabel(questionContent);
		questionLabel.setBorder(
			BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("The Question"),
				BorderFactory.createEmptyBorder(5, 5, 5, 5)));
		// this is a really hacky way of getting around the BorderLayout manager's whininess
			// (but it works)
		mainPanel.setBorder(
			BorderFactory.createEmptyBorder(0, 6, 0, 6));
			
		for (int i = 0; i < 4; i++)
		{
			JTextArea aa = new JTextArea();
			aa.setFont(new Font("Serif", Font.PLAIN, 12));
			answerArea.setLineWrap(true);
			answerArea.setWrapStyleWord(true);
			JScrollPane aP = new JScrollPane(aa);
			aP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			aP.setPreferredSize(new Dimension((GUI_WIDTH - 40), ((GUI_HEIGHT / 2) - 54)));
			aP.setMaximumSize(new Dimension((GUI_WIDTH - 40), ((GUI_HEIGHT / 2) - 54)));
			aP.setBorder(
				BorderFactory.createCompoundBorder(
					BorderFactory.createTitledBorder("Incorrect Answer"),
					BorderFactory.createEmptyBorder(3, 3, 3, 3)));
			answers.add(aP);
		}
		
		JButton backButton = new JButton("Back");
		backButton.addActionListener(new NewQuestionListener());
		buttons.add(backButton);
		
		JButton saveButton = new JButton("Save");
		//saveButton.addActionListener(new SaveListener());
		buttons.add(saveButton);
	
		// Organize panels
		centerPanel.add(BorderLayout.NORTH, questionLabel);
		centerPanel.add(BorderLayout.CENTER, answers);
		
		// Add components to the panels
		explanation.add(explanationLabel);
		
		// Add top-level panels to the GUI
		mainPanel.add(BorderLayout.NORTH, explanation);
		mainPanel.add(BorderLayout.CENTER, centerPanel);
		mainPanel.add(BorderLayout.SOUTH, buttons);
		
		// Refresh
		mainPanel.setVisible(false);
		mainPanel.setVisible(true);
	}
	
	public void cleanGUI()
	{
		mainPanel.setVisible(false);
		mainPanel.removeAll();
		mainPanel.setBorder(
			BorderFactory.createEmptyBorder(0,0,0,0));
		mainPanel.setVisible(true);
	}
	
	//--- ACTION LISTENERS ---
	class CleaningListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			cleanGUI();
		}
	}
	
	class AboutListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			JOptionPane.showMessageDialog(mainWindow,
				"<html>Created by Eric Vernon <br /><br />Licensed under the GNU<br /> General Public License v3<br /><br />Inspired by my snooze button</html>",
				"About jReview",
				JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	class QuitListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			System.exit(0);
		}
	}
	
	class NewQuestionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			newQuestionGUI();
		}
	}
	class MCEnterListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
				// Save the question
				questionContent = questionArea.getText();
				questionAnswer = answerArea.getText();
				
				// And advance to the next screen
				cleanGUI();
				MCEnterGUI();
		}
	}
}